package dpyNotepad;
import java.util.Stack;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class Notepad extends MIDlet implements CommandListener {

	private Display display;
	private Splash  splash;
	
	public Command exitCmd;
	public Command openPadCmd;
	public Command addPageCmd;
	public Command mainMenuCmd;
	public Command goFolderUpCmd;
	public Command inputFileNameCmd;
	
	private TextPage textPageScr;
	private MainMenu mainMenuScr;
	private FolderExplorer folderExplorerScr;
	private InputFileName inputFileNameScr;
	private Encodings encodingsScr;
	
	public String folderPath;
	public String fileName;
	public Stack roots;
	public String encoding;
	
	public String getTextPageText(){
		return this.textPageScr.getString();
	}
	
	public void setTextPageText(String text){
		int length = text.length();
		int pages = (length / TextPage.PAGE_SIZE) + 1;
		int size = pages * TextPage.PAGE_SIZE;
		
		this.textPageScr = new TextPage("Text page", text, size,
				TextField.ANY, this);
	}
	
	public String getSavePath(){
		return "file:///" + this.folderPath + this.fileName;
	}
	
	public boolean hasValidPath(){
		return this.folderPath != "" && this.fileName != "";
	}
	
	public Notepad() {
		this.display = Display.getDisplay(this);
		this.splash = new Splash("/DPY1.jpg", 0x333333);
	}

	protected void startApp() throws MIDletStateChangeException {
		
		this.folderPath = "";
		this.fileName = "";
		this.encoding = Encodings.ASCII;
		this.roots = new Stack();
		
		this.exitCmd = new Command("Exit", Command.EXIT, 2);
		this.openPadCmd = new Command("Go to notebook", Command.SCREEN, 1);		
		this.addPageCmd = new Command("Add page size", Command.SCREEN, 1);
		this.mainMenuCmd = new Command("Main menu", Command.EXIT, 2);
		this.goFolderUpCmd = new Command("Go folder up", Command.BACK, 1);
		this.inputFileNameCmd = new Command("Submit filename", Command.BACK, 1);
				
		this.splash.show(this.display, this.getMainMenuScreen(), 1500);
	}	
	
	private TextPage getTextPageScreen() {
		if(this.textPageScr == null){
			this.textPageScr = 
					new TextPage("Text page", "", TextPage.PAGE_SIZE,
							TextField.ANY, this);
		}
		
		return 	this.textPageScr;
	}
	
	private InputFileName getInputFileNameScreen(){
		this.inputFileNameScr = new InputFileName("Input file name!", this);
		
		return this.inputFileNameScr;
	}
	
	private MainMenu getMainMenuScreen(){
		if(this.mainMenuScr == null){
			this.mainMenuScr = new MainMenu("Main navigation menu", List.IMPLICIT, this);
		}
		
		this.setTicker(this.mainMenuScr);
		
		return this.mainMenuScr;
	}
	
	private Encodings getEncodingsScreen(){
		if(this.encodingsScr == null){
			this.encodingsScr = 
					new Encodings("Choose encoding for import/export!", List.IMPLICIT, this);
		}
		
		return this.encodingsScr;
	}
	
	public void setTicker(Displayable screen){
		screen.setTicker(new Ticker(this.getTickerText()));
	}
	
	public String getTickerText(){
		return (this.folderPath != "" ? this.folderPath : "<Folder not selected>/")
				+ (this.fileName != "" ? this.fileName : "<File not selected>");
	}
	
	private FolderExplorer getFolderExplorerScreen(){
		if(this.folderExplorerScr == null){
			this.getNewFolderExplorerScreen();
		}
		
		return this.folderExplorerScr;
	}
	
	private FolderExplorer getNewFolderExplorerScreen(){
		this.folderExplorerScr = new FolderExplorer("Select file/folder", List.IMPLICIT, this);
		
		return this.folderExplorerScr;
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command command, Displayable displayable) {
		if (command == this.exitCmd) {
			this.exitExecute();
		}
		else if(command == this.openPadCmd){
			this.goToNotepadExecute();
		}
		else if(command == this.mainMenuCmd){
			this.goToMainMenuExecute();
		}
		else if(command == this.addPageCmd){
			this.textPageScr.setMaxSize(this.textPageScr.getMaxSize() + TextPage.PAGE_SIZE);
			this.goToMainMenuExecute();
			this.goToNotepadExecute();
		}
		else if(command == this.inputFileNameCmd){
			this.fileName = this.inputFileNameScr.textfield.getString();
			this.goToMainMenuExecute();
		}
		else if(command == List.SELECT_COMMAND){
			if(this.mainMenuScr != null && this.mainMenuScr.isShown()){
				this.getTextPageScreen();
				this.mainMenuScr.handleCommand();
			}
			else if(this.folderExplorerScr != null && this.folderExplorerScr.isShown()){
				this.folderExplorerScr.handleCommand();
			}
			else if(this.encodingsScr != null && this.encodingsScr.isShown()){
				this.encodingsScr.handleCommand();
			}
		}
	}
	
	private void exitExecute(){
		try {
			this.destroyApp(true);
		} catch (MIDletStateChangeException e) {
			e.printStackTrace();
		} finally {				
			this.notifyDestroyed();
		}
	}
	
	public void goToNotepadExecute(){
		this.display.setCurrent(getTextPageScreen());
	}
	
	public void goToMainMenuExecute(){
		this.display.setCurrent(getMainMenuScreen());
	}
	
	public void goToEncodingsExecute(){
		this.display.setCurrent(this.getEncodingsScreen());
	}
	
	public void goToFolderExplorerScreen(){
		this.display.setCurrent(getFolderExplorerScreen());
	}
	
	public void goToInputFileNameScreen(){
		this.display.setCurrent(this.getInputFileNameScreen());
	}
	
	public void goToNewFolderExplorerScreen(){
		this.display.setCurrent(this.getNewFolderExplorerScreen());
	}
	
	public void showAlert(String message){
		this.display.setCurrent(new Alert(message));
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {}
}
